<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  function getMonthDays(date) {
    var __date = new Date(date);

    // 方法1
    var year = __date.getFullYear();
    var month = __date.getMonth() + 1;

    console.log(year, month)
    var days = new Date(year, month, 0);
    return days.getDate()
  }

  console.log(getMonthDays('2022-01'))
  console.log(getMonthDays('2022-02'))
  console.log(getMonthDays('2022-03'))

  function getMonthDays2(date) {
    var __date = new Date(date);

    // 方法2
    __date.setMonth(__date.getMonth() + 1)
    __date.setDate(0)
    return __date.getDate()
  }

  console.log(getMonthDays2('2022-01'))
  console.log(getMonthDays2('2022-02'))
  console.log(getMonthDays2('2022-03'))
</script>
</body>
</html>
